<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        图片Base64上传
        <input type="file" multiple="multiple" id="img_upload" accept="image/png,image/jpg">
    </div> 
    <script>
        document.getElementById('img_upload').addEventListener('change', function () {
            for (let i = 0; i < this.files.length; i++) {
                var reader = new FileReader();
                var type = this.files[i].type;
                reader.onload = function () {
                    console.log(this.result, type); // 默认base64
                    convertImgToBase64(this.result, type);
                };  
                reader.readAsDataURL(this.files[i]);
            }
        });
        // 压缩图片
        function convertImgToBase64 (bigImg, type) {
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            var img = new Image();
            img.onload = function () {
                var width = img.width;
                var height = img.height;

                canvas.width = width/2;
                canvas.height = height/2;
                ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                var newURL = canvas.toDataURL(type)
                console.log('这就是压缩后的base64', newURL); 
            };
            img.src = bigImg;
        }
    </script>
</body>
</html>